<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/11
 * Time: 8:49
 */

namespace App\Http\Controllers\Api\V1;



use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\AdService;

class AdController extends Controller{

    /**
     * @api {post} /get_ads/{position_key} 获取广告
     * @apiName Ad/getAds
     * @apiGroup Ad
     * @apiDescription 根据位置标识获取广告
     * @apiParam {string} position_key  广告位标识
     * @apiSuccess {Object[]}  Object   广告信息
     * @apiSuccess {int}  Object.id   分类ID
     * @apiSuccess {int}  Object.pos_id   广告位置ID
     * @apiSuccess {string}  Object.title   广告标题
     * @apiSuccess {string}  Object.image   广告图片
     * @apiSuccess {string}  Object.url   广告链接
     * @apiSuccess {string}  Object.m_url   移动端广告链接
     * @apiSuccess {int}  Object.list_order   排序
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
        "id": 53,
        "pos_id": 3,
        "title": "测试手机端广告url",
        "image": "05d1cc39360471de32973fac66463751",
        "m_image": "",
        "url": "www.baidu.com",
        "m_url": "m.afuyigou.w3php.com/activity/index_1",
        "list_order": 0,
        "mobile_image": null
        }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少参数position_key"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该广告位下没有广告"
    }
     */

    public function getAds($position_key,AdService $adService){
        if(empty($position_key)){
            throw new ApiInputErrorException('缺少参数position_key');
        }

        $ads = $adService->get($position_key);
        if($ads){
            return response()->json($ads);
        }else{
            throw new ApiDoneErrorException('该广告位下没有广告');
        }
    }

} 